package com.zhijiaoyun.controller;

import com.zhijiaoyun.common.DsConstant;
import com.zhijiaoyun.controller.base.BaseController;
import com.zhijiaoyun.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author shisfish
 * @date 2017/6/6
 */
@Controller
@RequestMapping(value = "user", produces = "application/json; charset=utf-8")
public class UserController extends BaseController {

    /**
     * 未有此功能
     *
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/frozenUser")
    public Map<String, Object> frozenUser(Long userId) {
        Map<String, Object> map = new HashMap<>();
        User user = userService.getUserByUserId(userId);
        if (user == null) {
            map.put(DsConstant.MESSAGE, "用户不存在");
            map.put(DsConstant.CODE, 1);
            return map;
        }
        user.setStatus(1);
        userService.updataUser(user);

        // TODO 系统消息 通知

        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    /**
     * 用户列表
     *
     * @param pageIndex
     * @param username
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/userList")
    public Map<String, Object> userList(int pageIndex, int pageSize, String username) {
        if (pageIndex <= 0) {
            pageIndex = 1;
        }
        if (pageSize <= 0) {
            pageSize = DsConstant.PAGESIZE_DEFAULT;
        }
        return userService.pageByUserName(pageIndex, pageSize, username);
    }

    @ResponseBody
    @RequestMapping(value = "/courseHistoryList/{userId}")
    public Map<String, Object> courseHistoryList(@PathVariable("userId") long userId, int pageIndex, int pageSize) {
        if (pageIndex <= 0) {
            pageIndex = 1;
        }
        if (pageSize <= 0) {
            pageSize = DsConstant.PAGESIZE_DEFAULT;
        }
        return learnHistoryService.pageCourseHistoryDtoByUserId(pageIndex, pageSize, userId);
    }

    // 我的学习历史二级
    @ResponseBody
    @RequestMapping(value = "/learnHistoryList/{courseId}/{userId}")
    public Map<String, Object> learnHistoryList(@PathVariable("userId") long userId, @PathVariable("courseId") long courseId) {
        return learnHistoryService.listLearnHistoryDtoByCourseIdUserId(userId, courseId);
    }

}
